#!/usr/bin/ruby -w 
#The program continuosly scans the "monitor" database in idb1
#where results of packet monitoring by tcpdump program are 
#stored by database.rb program.
#The program compares source ip address acquired from packet table of monitor database
#with expected source ip address and then compares expected channel with the current channel from where packet 
#is received. If mismatch is found, program reports spillover event.
#
#Author::Dipti Vete, Akshay Jog(mail to: dvete@eden.rutgers.edu, akshayj@eden.rutgers.edu)
#
#License::Distributes under the same terms as ruby


begin

require "mysql"
class Analyze   
hash = {}
file = File.open("/home/initdata.txt", "r")
while(line = file.gets)
  line.strip!
  num =  line.split(" ")
  hash[num[0]] = num[1]
end
  
# Connect to the Database
dbm = Mysql.new("idb1","orbit", "orbit")
dbm.select_db("tripti")
def flag=( value )
    @flag = value
end

notify = analyze.new()
notify.flag ="0'

results = dbm.query("SELECT channel, src_mac FROM packet")
while curr_row=results.fetch_row do
             
       if hash.has_value?(curr_row[0])
            if (hash[curr_row[1]] != curr_row[0])
              puts "spillover detected"
	      puts "Packet received from src_mac: #{curr_row[1]} on channel: #{curr_row[0]} expected channel: #{hash[curr_row[1]]}"
              notify.flag = "1" #set flag = 1 if spillover occurs for notification subsystem
            else
              puts "no spillover"
            end
       else
           puts "spillover detected"
           puts "Packet received from src_mac: #{curr_row[1]} on channel: #{curr_row[0]} expected channel: #{hash[curr_row[1]]}"
           notify.flag = "1"
           
       end
end

dbm.query("TRUNCATE TABLE packet")

dbm.close if dbm

end  #class Analyze ends here

end # code ends here



